<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* 实训
*/
class School extends MY_Controller {
	function join(){
		if(!$_POST){
			print_r($_COOKIE);exit;
		}
		$this->db->trans_start();
		$post_data = $this->input->post();

		if(empty($post_data['mobile'])){
			show_error_message("http://www.dax123.com/sqrz.html", "手机号码不能为空!");
			exit;
		}

		if(empty($post_data['school_name'])){
			show_error_message("http://www.dax123.com/sqrz.html", "入驻名称不能为空!");
			exit;
		}

		if(empty($post_data['url_web'])){
			show_error_message("http://www.dax123.com/sqrz.html", "入驻域名不能为空!");
			exit;
		}

		$user_info = get_field_by_where("user", "*", "mobile='{$post_data['mobile']}'");
		if($user_info){
			show_error_message("http://www.dax123.com/sqrz.html", "您填写的手机号码已注册!");
			exit;
		}

		$school_info = get_field_by_where("o2o_school", "*", "url_web='{$post_data['url_web']}'");
		if($user_info){
			show_error_message("http://www.dax123.com/sqrz.html", "您填写的域名已注册!");
			exit;
		}

		/*if($post_data['code'] != $_COOKIE['rzcode'] || empty($_COOKIE['rzcode'])){
			show_error_message("http://www.dax123.com/sqrz.html", "您输入的验证码有误，或者验证码已过期!");
			exit;
		}*/

		$school_data['rz_type'] = $post_data['rz_type'];
		$school_data['school_name'] = $post_data['school_name'];
		$school_data['url_web'] = $post_data['url_web'];
		$school_data['link_phone'] = $post_data['mobile'];
		$school_data['run_status'] = $school_data['status'] = 0;
		$school_data['create_on'] = time();
		$this->db->insert("o2o_school", $school_data);
		$school_id  =$this->db->insert_id();

		$user_data['add_time'] = $school_data['create_on'];
		$user_data['school_id'] = $school_id;
		$user_data['mobile'] = $post_data['mobile'];
		$md5_pass = md5($post_data['password']);
		$rand_str = '';
		for($i=0; $i<10; $i++){
			$rand_str .= substr($md5_pass, rand(0, strlen($md5_pass)-1), 1);
		}
		$user_data['rand_str'] = $rand_str;
		$user_data['password'] = md5($md5_pass.$rand_str);
		$user_data['register_ip'] = $_SERVER["REMOTE_ADDR"];
		$user_data['stream_id'] = create_rand_str(18, 18);
		$user_data['is_teacher'] = 1;
		$this->db->insert("user", $user_data);

		$teacher_data['user_id'] = $this->db->insert_id();
		$teacher_data['school_id'] = $school_id;
		$teacher_data['school_name'] = $post_data['school_name'];
		$teacher_data['mobile'] = $post_data['mobile'];
		$teacher_data['allow_live'] = 1;
		$teacher_data['stream_id'] = $user_data['stream_id'];
		$teacher_data['is_manager'] = 1;
		$teacher_data['add_time'] = $user_data['add_time'];
		$this->db->insert("o2o_teachers", $teacher_data);
		$teacher_id = $this->db->insert_id();
		$this->db->trans_complete();
		show_message("http://www.520dax.com", "入驻申请提交成，请等待客服开通并联系!");
	}
}